﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Net.Mail;
using BskLeague.Models;
using System.Security.Cryptography;
using System.Text;

namespace BskLeague.Controllers
{
    public class AdvertController : Controller
    {
        BskLeagueDB _db = new BskLeagueDB();

        public ActionResult Index()
        {
            var adverts = (from m in _db.Adverts
                            where m.advertActive == true
                            select m).ToList();

            return View(adverts);
        }

        public ActionResult Details(int id)
        {
            var advert = _db.Adverts.Find(id);
            return View(advert);
        }

        [Authorize(Roles = "Admin")]
        public ActionResult AdminRemove(int id)
        {
            var advert = _db.Adverts.Find(id);
            advert.advertActive = false;

            _db.Entry(advert).State = System.Data.EntityState.Modified;
            _db.SaveChanges();

            return RedirectToAction("Index");
        }

        public ActionResult Delete(int id, string delKey)
        {
            var advert = _db.Adverts.Find(id);
            advert.advertActive = false;

            _db.Entry(advert).State = System.Data.EntityState.Modified;
            _db.SaveChanges();

            return RedirectToAction("Index");
        }

        public ActionResult New()
        {
            return View();
        }

        [HttpPost]
        public ActionResult Create(string advertTitle, string advertContent, string advertMail)
        {
            Advert advert = new Advert();
            advert.advertActive = false;
            advert.advertTitle = advertTitle;
            advert.advertContent = advertContent;
            advert.advertMail = advertMail;
            advert.advertActKey = GetMD5Hash(advertTitle + advertMail + "act");
            advert.advertDelKey = GetMD5Hash(advertTitle + advertMail + "del");
            advert.advertDate = DateTime.Now;

            _db.Adverts.Add(advert);
            _db.SaveChanges();

            SendActivationEmail(advert);

            return View();
        }

        internal static void SendActivationEmail(Advert advert)
        {
            MailMessage mailMessage = new MailMessage(new MailAddress("basket.league.123@gmail.com"), new MailAddress(advert.advertMail));
            mailMessage.Subject = "Aktywacja ogłoszenia";
            mailMessage.IsBodyHtml = true;
            string mailContent = "Witaj,<br /><br />wystawiłeś ogłoszenie zatytułowane:<br /><i>";
            mailContent += advert.advertTitle;
            mailContent += "</b><br /><br />o treści:<br /><i>";
            mailContent += advert.advertContent;
            mailContent += "</i><br /><br />Aby aktywować wystawione ogłoszenie kliknij <a href='http://localhost:2674/Advert/Activate?id=";
            mailContent += advert.advertId;
            mailContent += "&actKey=";
            mailContent += advert.advertActKey;
            mailContent += "'>odnośnik</a>.<br />Aby usunąć wystawione ogłoszenie kliknij <a href='http://localhost:2674/Advert/Delete?id=";
            mailContent += advert.advertId;
            mailContent += "&delKey=";
            mailContent += advert.advertDelKey;
            mailContent += "'>odnośnik</a>.";
            mailMessage.Body = mailContent;
                /*+ advert.advertActKey + "'>" + advert.advertActKey + "</a><br /><br />Aby usunąć ogłoszenie kliknij poniższy link:"
                + "<br /><a href='" + advert.advertDelKey + "'>" + advert.advertDelKey + "</a><br />";*/

            System.Net.NetworkCredential networkCredentials = new System.Net.NetworkCredential("basket.league.123@gmail.com", "tajnehaslo");

            SmtpClient smtpClient = new SmtpClient();
            smtpClient.EnableSsl = true;
            smtpClient.UseDefaultCredentials = false;
            smtpClient.Credentials = networkCredentials;
            smtpClient.Host = "smtp.gmail.com";
            smtpClient.Port = 587;
            smtpClient.Send(mailMessage);
        }

        private string GetMD5Hash(string value)
        {
            
            return Convert.ToBase64String(new MD5CryptoServiceProvider().ComputeHash(new UTF8Encoding().GetBytes(value))).ToLower();
        }

        public ActionResult Activate(int id, string actKey)
        {
            string alert;
            var advert = _db.Adverts.Find(id);
            if (advert == null)
            {
                alert = "Brak ogłoszenia o podanym ID.";
            }
            else if (advert.advertActKey.Equals(actKey))
            {
                advert.advertActive = true;
                _db.Entry(advert).State = System.Data.EntityState.Modified;
                _db.SaveChanges();
                alert = "Twoje ogłoszenie zostało aktywowane.";
            }
            else
            {
                alert = "Podano błędny klucz autoryzujący.";
            }

            return View("Activate", model: alert);
        }
    }
}
